package com.gears_software.sxdc.dccx.ui.fragment;

import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.adapter.MyTripListAdapter;
import com.gears_software.sxdc.dccx.data.finder.ICPOrderFinder;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.MyTripModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.ui.util.MaterialDialogProgress;
import com.github.mzule.activityrouter.annotation.Router;
import com.github.mzule.activityrouter.router.Routers;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.androidannotations.rest.spring.annotations.RestService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chezhenzhen on 2017/8/16.
 */

@EFragment(R.layout.fragment_mytrip_carpool)
public class MyTripCarPoolFragment extends Fragment {
    @ViewById
    ListView list_trip;
    @ViewById
    RelativeLayout re_wu;
    @ViewById
    TextView txt_nodata_icon;
    @RestService
    ICPOrderFinder orderFinder;
    @Bean
    GearsErrorHandler errorHandler;
    @App
    DCCXApplication app;
    MyTripListAdapter adapter;
    List<MyTripModel> list = new ArrayList<MyTripModel>();
    MaterialDialogProgress progress;

    @AfterViews
    void created() {
        orderFinder.setRestErrorHandler(errorHandler);
        txt_nodata_icon.setTypeface(app.iconfont);
        progress = new MaterialDialogProgress(getActivity());
        progress.show("正在加载...");
        getTripList();
    }

    @Background
    void getTripList() {
        ResultModel<List<MyTripModel>> res = orderFinder.getMyCarPoolOrder();
        showResult(res);
    }

    @UiThread
    void showResult(ResultModel<List<MyTripModel>> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    list = res.getData();
                    adapter = new MyTripListAdapter(list, getActivity());
                    list_trip.setAdapter(adapter);
                    if (list == null || list.size() == 0){
                        list_trip.setVisibility(View.GONE);
                        re_wu.setVisibility(View.VISIBLE);
                    }else{
                        list_trip.setVisibility(View.VISIBLE);
                        re_wu.setVisibility(View.GONE);
                    }
                }
            }
        }
        progress.dismiss();
    }

    @ItemClick
    void list_trip(MyTripModel model) {
        if (model.getFlag() == 1) {
            Routers.open(getActivity(), "gears_dccx://tripinfo/" + model.getId());
        } else {
            Routers.open(getActivity(), "gears_dccx://passengertrip/"+model.getId());
        }

    }
}
